//
//  ZJBaseService.m
//  Project
//
//  Created by 郑键 on 17/2/15.
//  Copyright © 2017年 zhengjian. All rights reserved.
//

#import "ZJBaseService.h"

@implementation ZJBaseService

+ (instancetype)share{
    static dispatch_once_t t;
    static ZJBaseService *service       = nil;
    dispatch_once(&t, ^{
        service                         = [[ZJBaseService alloc] init];
    });
    return service;
}

/**
 *  请求数据成功的处理
 *
 *  @param dictionary                   请求回来的数据
 *  @param requestInfoModel             请求task
 *  @param sucBlock                     如果一切正常则会执行该block
 *  @param failBlock                    出现其它错误时执行(例如:请求虽然成功,但出现必填参数为空)
 */
- (void)handleRequestSuccess:(NSDictionary *)dictionary
                requestModel:(ZJProjectNetRequestInfo *)requestInfoModel
                    sucBlock:(ZJNetRequestSuccessBlock)sucBlock
                   failBlock:(ZJNetRequestFailureBlock)failBlock
{
    /**
     *  请求成功，返回的数据判断，当返回数据为空或者无数据时，抛出无数据异常，根据
     */
    if (!dictionary || [[dictionary allKeys] count]==0) {
        if (failBlock) {
            
            /**
             *  TODO：添加统计锚点
             */
            
            /**
             *  初始化状态码，失败回调
             */
            ZJNetRequestError *error                              = [self errorWithErrorInfo:@""
                                                                                      domain:@""
                                                                                        code:-1];
            failBlock (error, requestInfoModel);
        }
        return;
    }
    
    /**
     *  TODO:获取与后台约定状态码，通过状态码判断是否请求成功
     *  当前status为服务器返回的JSON对象中
     *  需根据服务器具体情况来确定从哪里获取status
     */
    NSString *status = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"status"]];
    if ([status isEqualToString:@"100"]) {
        if (dictionary) {
            if (sucBlock){
                sucBlock (dictionary,requestInfoModel);
            }
        }else{
            if (failBlock) {
                
                /**
                 *  TODO:初始化错误信息，失败回调
                 */
                ZJNetRequestError *error                          = [self errorWithErrorInfo:@""
                                                                                      domain:@""
                                                                                        code:-1];
                failBlock (error,requestInfoModel);
            }
        }
        return;
    }
    
    /**
     *  TODO:其他异常，根据服务器情况来写判断条件
     *  从服务器获取错误信息
     */
    if (dictionary && ([dictionary objectForKey:@"status"]
                       || [dictionary objectForKey:@"msg"])) {
        if (failBlock) {
            
            /**
             *  TODO:初始化错误信息，失败回调
             */
            ZJNetRequestError *error                              = [self errorWithErrorInfo:@""
                                                                                      domain:@""
                                                                                        code:-1];
            failBlock (error,requestInfoModel);
            return;
        }
    }
    return;
    
}

/**
 *  请求数据失败后的处理
 *
 *  @param error     请求失败后返回Error对象
 */
- (void)handleRequestFailed:(NSError *)error
               requestModel:(ZJProjectNetRequestInfo *)requestInfoModel
                  failBlock:(ZJNetRequestFailureBlock)failBlock
{
    if (failBlock) {
        
        /**
         *  TODO:判断当为用户手动取消时，不抛出异常
         */
        
        NSError *customError;
        
        /**
         *  TODO:根据状态码来初始化不同的异常
         */
        failBlock (customError,requestInfoModel);
    }
    
}

/**
 *  初始化error的方法封装
 *
 *  @param errorInfo                错误信息
 *  @param domain                   描述
 *  @param code                     错误状态吗
 *  @return                         error对象
 */
- (ZJNetRequestError *)errorWithErrorInfo:(NSString *)errorInfo
                                   domain:(NSString *)domain
                                     code:(NSInteger)code
{
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:errorInfo
                                                         forKey:NETREQUEST_ERRORINFO_KEY];
    ZJNetRequestError *resultError = [[ZJNetRequestError alloc] initWithDomain:domain
                                                                          code:code
                                                                      userInfo:userInfo];
    return resultError;
}

@end
